package com.doris.media.picker.widget

import android.content.Context
import android.content.res.ColorStateList
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import androidx.constraintlayout.widget.ConstraintLayout
import com.doris.media.picker.R
import kotlinx.android.synthetic.main.media_picker_loading_view.view.*

class LoadingView : ConstraintLayout {

    constructor(context: Context) : super(context) {
        init()
    }

    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        init()
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) :
            super(context, attrs, defStyleAttr) {
        init()
    }

    private fun init() {
        LayoutInflater.from(context)
            .inflate(R.layout.media_picker_loading_view, this, true)
    }

    fun setProgressColor(color: Int) {
        mediaPicker_progress.indeterminateTintList = ColorStateList.valueOf(color)
    }

    fun setTipColor(color: Int) {
        mediaPicker_tip.setTextColor(color)
    }

    fun setPermissionBtnColor(color: Int) {
        mediaPicker_btn.setTextColor(color)
    }

    fun setPermissionBtnBorderColor(color: Int) {
        mediaPicker_btn.setStrokeColors(ColorStateList.valueOf(color))
    }

    fun hide() {
        visibility = View.GONE
    }

    fun showLoading() {
        showLoading("正在加载")
    }

    fun showLoading(tip: String) {
        visibility = View.VISIBLE

        mediaPicker_tip.text = tip
        mediaPicker_tip.visibility = View.VISIBLE
        mediaPicker_progress.visibility = View.VISIBLE

        mediaPicker_btn.visibility = View.GONE
    }

    fun showTip(tip: String) {
        visibility = View.VISIBLE

        mediaPicker_tip.text = tip
        mediaPicker_tip.visibility = View.VISIBLE

        mediaPicker_progress.visibility = View.GONE
        mediaPicker_btn.visibility = View.GONE
    }

    fun showNoPermission() {
        showTipBtn("无法访问本地存储", "去授权")
    }

    fun showNoPermission(tips: String) {
        showTipBtn(tips, "去授权")
    }

    fun showTipBtn(tip: String, btn: String) {
        visibility = View.VISIBLE

        mediaPicker_tip.text = tip
        mediaPicker_tip.visibility = View.VISIBLE

        mediaPicker_btn.text = btn
        mediaPicker_btn.visibility = View.VISIBLE

        mediaPicker_progress.visibility = View.GONE
    }

    fun setBtnClickListener(listener: OnClickListener) {
        mediaPicker_btn.setOnClickListener(listener)
    }

    fun setTip(tip: String) {
        if (tip.isEmpty()) {
            mediaPicker_tip1.visibility = View.GONE
        } else {
            mediaPicker_tip1.text = tip
            mediaPicker_tip1.visibility = View.VISIBLE
        }
    }
}